package com.baihui.taobao.db;

import java.lang.reflect.Field;
import java.util.Date;

import com.baihui.taobao.authorize.TaoBaoSessionInfo;
import com.taobao.api.domain.User;

/**
 * Created by Kevin.
 * User: HSZM1106-0312
 * Date: 12-1-3
 * Time: 下午8:43
 */
public class TPSessionDAO extends GeneDAO {

    public void insert(User userInfo, TaoBaoSessionInfo sessionInfo) throws Exception {
        if(sessionInfo==null || sessionInfo.getTopParameter()==null||userInfo==null) {
            throw new Exception("Session或者User为空，不能新增用户！");
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("UserId = " + userInfo.getUserId());
        stringBuffer.append(",Uid = " + userInfo.getUid());
        stringBuffer.append(",Visitor_nick = " + sessionInfo.getVisitor_nick());
        stringBuffer.append(",AlipayNo= " + userInfo.getAlipayNo());
        stringBuffer.append(",AlipayAccount = " + userInfo.getAlipayAccount());
        stringBuffer.append(",TopSession = " + sessionInfo.getTopSession());
        stringBuffer.append(",Expires_in = " + sessionInfo.getExpires_in());
        stringBuffer.append(",TopParameter().getTs = " + sessionInfo.getTopParameter().getTs());
        stringBuffer.append(",Sub_visitor_id = " + sessionInfo.getSub_visitor_id());
        stringBuffer.append(",Sub_visitor_nick = " + sessionInfo.getSub_visitor_nick());
        stringBuffer.append(",TopParameter().getRe_expires_in = " + sessionInfo.getTopParameter().getRe_expires_in());
        System.out.println("sssssssssssssssssssssss=" + stringBuffer.toString());
        String sql = "insert into topsession(user_id,uid,nick,sex,email,alipay_no,alipay_account,sessionid,expires_in,ts,sub_visitor_id,sub_visitor_nick,iframe,re_expires_in,createTime,appName) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        updateAndInsert(sql, new Object[]{
                userInfo.getUserId(),
                userInfo.getUid(),
                sessionInfo.getVisitor_nick(),
                userInfo.getSex(),
                userInfo.getEmail(),
                userInfo.getAlipayNo(),
                userInfo.getAlipayAccount(),
                sessionInfo.getTopSession(),
                sessionInfo.getExpires_in(),
                sessionInfo.getTopParameter().getTs(),
                sessionInfo.getSub_visitor_id(),
                sessionInfo.getSub_visitor_nick(),
                sessionInfo.getTopParameter().getIframe(),
                sessionInfo.getTopParameter().getRe_expires_in(),
                new Date(),
                "taobaocrm"
        });
    }


    public void update(User userInfo, TaoBaoSessionInfo taoBaoSessionInfo) throws Exception {
        String sql = "update topsession set nick=?,email=?,sessionid=?,expires_in=?,ts=?,sub_visitor_nick=? where user_id=? and appName = 'taobaocrm' ";
        updateAndInsert(sql, new Object[]{
                taoBaoSessionInfo.getVisitor_nick(),
                userInfo.getEmail(),
                taoBaoSessionInfo.getTopSession(),
                taoBaoSessionInfo.getExpires_in(),
                taoBaoSessionInfo.getTopParameter().getTs(),
                taoBaoSessionInfo.getSub_visitor_nick(),
                userInfo.getUserId()});
    }


    @Override
    protected String getPrimaryKey(Class aClass) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Class getDomainClass() {
        return User.class;
    }

    @Override
    protected String parseRelationType(Field field) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Object parseValue(Object value, Field field) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
